package org.digitarts.rtms.spring.collectors.shell.telnet;

import java.io.IOException;
import java.util.Map;

import org.digitarts.rtms.spring.collectors.shell.IRemoteShell;

import de.mud.telnet.TelnetWrapper;

public class TelnetRemoteShell extends TelnetWrapper implements IRemoteShell
{
	protected String	tPrompt				= null;
	protected String	userNamePrompt		= null;
	protected String	passwordPrompt		= null;
	protected String	shellIntialization	= null;
	protected String	iPAddress			= null;
	protected String	userName			= null;
	protected String	password			= null;
	protected String	systemPrompt		= null;

	public TelnetRemoteShell()
	{

	}

	public TelnetRemoteShell(String userNamePrompt, String passwordPrompt, String shellPrompt)
	{
		this.systemPrompt = shellPrompt;
		this.userNamePrompt = userNamePrompt;
		this.passwordPrompt = passwordPrompt;
	}

	public void login(String user, String pwd) throws IOException
	{
		byte[] buffer = new byte[10240];
		waitfor(userNamePrompt);
		send(user);
		waitfor(passwordPrompt);
		send(pwd);
		read(buffer);
		String s = new String(buffer);
		if (s.contains(systemPrompt))
		{
			return;
		}
		else
		{
			throw new IOException("Telnet Login Failed");
		}
	}

	public void close()
	{
		try
		{
			this.disconnect();
		}
		catch (Exception e)
		{

		}
	}

	public void init() throws Exception
	{
		connect(iPAddress, port);
		login(userName, password);
		setPrompt(systemPrompt);
		send(shellIntialization);
	}

	public void setPort(int port)
	{
		this.port = port;
	}

	public String issueOSCommand(String command, Map<String, ?>... context) throws Exception
	{
		return issueOSCommand(command, 0, context);
	}

	public String issueOSCommand(String command, long timeOut, Map<String, ?>... context) throws Exception
	{
		return this.send(command);
	}

	public String getShellIntialization()
	{
		return shellIntialization;
	}

	public void setShellIntialization(String shellIntialization)
	{
		this.shellIntialization = shellIntialization;
	}

	public String getTPrompt()
	{
		return tPrompt;
	}

	public void setTPrompt(String prompt)
	{
		tPrompt = prompt;
	}

	public String getUserNamePrompt()
	{
		return userNamePrompt;
	}

	public void setUserNamePrompt(String userNamePrompt)
	{
		this.userNamePrompt = userNamePrompt;
	}

	public String getPasswordPrompt()
	{
		return passwordPrompt;
	}

	public void setPasswordPrompt(String passwordPrompt)
	{
		this.passwordPrompt = passwordPrompt;
	}

	public String getAddress()
	{
		return iPAddress;
	}

	public void setAddress(String address)
	{
		iPAddress = address;
	}

	public String getUserName()
	{
		return userName;
	}

	public void setUserName(String userName)
	{
		this.userName = userName;
	}

	public String getSystemPrompt()
	{
		return systemPrompt;
	}

	public void setSystemPrompt(String systemPrompt)
	{
		this.systemPrompt = systemPrompt;
	}

	public void setPassword(String password)
	{
		this.password = password;
	}

}
